<template>
  <div class="app-container">
    <el-card v-if="noticeData" class="notice-detail-card">
      <template #header>
        <div class="card-header">
          <span class="notice-title">{{ noticeData.title }}</span>
          <el-button type="primary" @click="handleBack">返回列表</el-button>
        </div>
      </template>

      <div class="notice-content">
        <!-- 通知基本信息 -->
        <el-descriptions title="通知基本信息" :column="2" border>
          <el-descriptions-item label="通知ID">{{ noticeData.noticeId }}</el-descriptions-item>
          <el-descriptions-item label="社区ID">{{ noticeData.name }}</el-descriptions-item>
          <el-descriptions-item label="通知类型">{{ noticeData.noticeType }}</el-descriptions-item>
          <el-descriptions-item label="发布人">{{ noticeData.publisher }}</el-descriptions-item>
          <el-descriptions-item label="发布时间">{{ noticeData.publishTime }}</el-descriptions-item>
          <el-descriptions-item label="接受范围">{{ noticeData.recipientScope }}</el-descriptions-item>
        </el-descriptions>

        <!-- 通知内容 -->
        <div class="content-section">
          <h3>通知内容</h3>
          <div class="content-html" v-html="noticeData.content"></div>
        </div>

        <!-- 附件信息 -->
        <div v-if="noticeData.attachments" class="attachments-section">
          <h3>附件信息</h3>
          <div class="attachments-content">
            {{ noticeData.attachments }}
          </div>
        </div>
      </div>
    </el-card>

    <el-card v-else-if="loading">
      <el-skeleton :rows="10" animated />
    </el-card>

    <el-empty v-else description="未找到通知信息" />
  </div>
</template>

<script setup>
import { ref, onMounted, watch, nextTick } from 'vue'
import { useRoute, useRouter, onBeforeRouteUpdate } from 'vue-router'

const route = useRoute()
const router = useRouter()

const noticeData = ref(null)
const loading = ref(false)

// 解析通知数据的函数
const parseNoticeData = async () => {
  loading.value = true

  // 使用 nextTick 确保 DOM 更新
  await nextTick()

  console.log('解析通知数据，当前路由参数:', route.query)

  if (route.query.noticeData) {
    try {
      noticeData.value = JSON.parse(route.query.noticeData)
      console.log('成功解析通知数据:', noticeData.value)
    } catch (error) {
      console.error('解析通知数据失败:', error)
      noticeData.value = null
    }
  } else {
    console.log('未找到 noticeData 参数')
    noticeData.value = null
  }

  loading.value = false
}

// 组件挂载时解析数据
onMounted(() => {
  parseNoticeData()
})

// 监听路由变化
onBeforeRouteUpdate((to, from, next) => {
  console.log('路由更新，从', from.path, '到', to.path)
  parseNoticeData()
  next()
})

// 监听 query 参数变化
watch(
    () => route.query.noticeData,
    (newVal, oldVal) => {
      if (newVal && newVal !== oldVal) {
        console.log('noticeData 参数发生变化')
        parseNoticeData()
      }
    },
    { immediate: true }
)

// 返回列表
const handleBack = () => {
  router.back()
}
</script>


<style scoped lang="scss">
.app-container {
  padding: 20px;
}

.notice-detail-card {
  margin: 0 auto;
  max-width: 1200px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.notice-title {
  font-size: 20px;
  font-weight: bold;
  color: #303133;
}

.notice-content {
  .content-section {
    margin-top: 20px;

    h3 {
      margin-bottom: 10px;
      color: #606266;
    }

    .content-html {
      padding: 15px;
      background: #f8f9fa;
      border-radius: 4px;
      border: 1px solid #e9ecef;
      min-height: 100px;
    }
  }

  .attachments-section {
    margin-top: 20px;

    h3 {
      margin-bottom: 10px;
      color: #606266;
    }

    .attachments-content {
      padding: 15px;
      background: #f8f9fa;
      border-radius: 4px;
      border: 1px solid #e9ecef;
    }
  }
}

:deep(.el-descriptions) {
  margin-bottom: 20px;
}

:deep(.el-descriptions__title) {
  font-size: 16px;
  font-weight: bold;
}
</style>